package siau.android.base

import android.content.Context
import android.util.TypedValue
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import hhm.android.library.utils.GetStringListener

class BottomSingleGenderSelectSheet(context: Context, var selectType: Int, private var listener: GetStringListener) : BottomSheetDialog(context) {

    companion object {
        const val SELECT_WOMAN = 2
        const val SELECT_MAN = 1
    }

    init {
        createView()
    }

    private fun createView() {
        val bottomSheetView: View = layoutInflater.inflate(R.layout.item_bottom_select_sex, null)
        setContentView(bottomSheetView)

        val motherIv: ImageView = bottomSheetView.findViewById(R.id.item_bottom_select_girl_iv)
        val fatherIv: ImageView = bottomSheetView.findViewById(R.id.item_bottom_select_boy_iv)

        when (selectType) {
            SELECT_WOMAN -> {
                motherIv.setImageResource(R.drawable.ring_main_5)
                fatherIv.setImageResource(R.drawable.ring_ca_1)
            }
            else -> {
                motherIv.setImageResource(R.drawable.ring_ca_1)
                fatherIv.setImageResource(R.drawable.ring_main_5)
            }
        }

        bottomSheetView.findViewById<LinearLayout>(R.id.item_bottom_select_boy).setOnClickListener {
            selectType = SELECT_MAN
            motherIv.setImageResource(R.drawable.ring_ca_1)
            fatherIv.setImageResource(R.drawable.ring_main_5)
            listener.getString(context.getString(R.string.boy))
            dismiss()
        }
        bottomSheetView.findViewById<LinearLayout>(R.id.item_bottom_select_girl).setOnClickListener {
            selectType = SELECT_WOMAN
            motherIv.setImageResource(R.drawable.ring_main_5)
            fatherIv.setImageResource(R.drawable.ring_ca_1)
            listener.getString(context.getString(R.string.girl))
            dismiss()
        }

        // 注意：这里要给layout的parent设置peekHeight，而不是在layout里给layout本身设置，下面设置背景色同理，坑爹！！！
        val bottomSheetBehavior: BottomSheetBehavior<*> = BottomSheetBehavior.from(
            bottomSheetView.parent as View
        )
        bottomSheetBehavior.peekHeight = dp2px(235f)
        //允许点击外部关闭，不允许滑动关闭
        bottomSheetBehavior.isHideable = false
        (bottomSheetView.parent as View).setBackgroundColor(context.resources.getColor(R.color.trans))
    }

    fun dp2px(dpValue: Float?): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, dpValue!!, context.resources.displayMetrics
        ).toInt()
    }
}